#include <stdio.h>
#include <math.h>
#include <string.h>
#define PI 3.14

void printHTTPResponseHeader();

void printHTMLHead( void );
void printHTMLStyles( void );
void printHTMLEnd( void );

int main( void )
{
    char* data;
    char tmp[100]; 
    char tmp1[100];
    char temp[100];
    char temp1[100];
    char * data1;
    data1 = "degree";
    char * data2;
    data2 = "minute";      
    int i = 0, j = 0, a = 0, k = 0, z = 0, b = 0;
    int odd;
    double degreeWithMinutes;
    double rad;

	int degreeInterval = 10;
	int minuteInterval = 6;

    printHTTPResponseHeader();
    printHTMLHead();
    data = (char *)getenv("QUERY_STRING");
    if(data != "")
    {
		while(data[k] != '=')
		{
			 temp[b] = data[k];
			 b++;
			 k++;
		}
        temp[b] = '\0';
		while((k < strlen(data)) && (data[k] != '&'))
		{
			tmp[a++] = data[++k];
		}
		tmp[a] = '\0';
		if(strcmp(temp,data1) == 0)
        { 
			degreeInterval = atoi(tmp);
			b=0;
			++k;
			while((data[k] != '=') && (k < strlen(data)))
			{
				temp[b] = data[k];
				b++;
				k++;
			}
			temp[b] = '\0';
			if((strcmp(temp,data2) == 0) && (k < (strlen(data)-1)))
			{
				++k;
				while((k < strlen(data)) && (data[k] != '&'))
				{
					tmp1[z] = data[k];
					++z;
					++k;
				}
				tmp1[z] = '\0';
				minuteInterval = atoi(tmp1);
			}
        }     
		else if(strcmp(temp, data2) == 0)
		{
			minuteInterval = atoi(tmp);
			b=0;
			++k;
			while((data[k] != '=') && (k < strlen(data)))
			{
				temp[b] = data[k];
				b++;
				k++;
			}
			temp[b] = '\0';
			if((strcmp(temp,data1) == 0) && (k < (strlen(data)-1)))
			{
				++k;
				while((k < strlen(data)) && (data[k] != '&'))
				{
					tmp1[z] = data[k];
					++z;
					++k;
				}
				tmp1[z] = '\0';
				degreeInterval = atoi(tmp1);
			}
		}
    }
	printf("<p>%d</p>\n", degreeInterval);
	printf("<p>%d</p>\n", minuteInterval);

   
    printf( "<table>\n" );
    odd = 1;
    for ( i = 0; i <= 360; i += degreeInterval )
    {        
        if ( odd && i > 0 )
        {
            printf( "<tr class=\"odd\">\n" );
        }
        else
        {
            printf( "<tr>\n" );
        }
        odd = 1 - odd;
        if ( i == 0 )
        {
            printf( "<td></td>\n" );
        }
        else
        {
            printf( "<td class=\"name\"><strong>%d</strong></td>", i );
        }
        for ( j = 0; j <= 60; j += minuteInterval )
        {
            degreeWithMinutes = i + j / 60.0;
            rad = degreeWithMinutes * PI / 180.0;
            if ( i == 0 )
            {
                printf( "<td class=\"name\"><strong>%d\'</strong></td>", j );
            }
            else
            {
                printf( "<td>%f</td>", sin( rad ) );
            }
        }
        printf( "\n</tr>\n" );
    }
    printf( "</table>\n");

    printHTMLEnd();
    
    return 0;
}
void printHTTPResponseHeader()
{
    printf( "Content-type: text/html\n\n");
}

void printHTMLHead(void)
{
    printf( "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
    printf( "<html>\n" );
    printf( "<head>\n" );
    printf( "<title>Sin table</title>\n" );
    printHTMLStyles();
    printf( "</head>\n" );
    printf( "<body>\n" );
}

void printHTMLStyles()
{
    printf( "<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style.css\" />\n" );
}

void printHTMLEnd(void)
{
    printf( "</body>\n" );
    printf( "</html>\n" );
}
